package com.cms.manage.util;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;
import java.util.Set;

public class MapUtil {
	
    public static Short getShort(Map<String, String> map, String key) {
        if (null == map || null == key || StringUtils.isBlank(map.get(key))) {
            return null;
        }
        return Short.valueOf(map.get(key));
    }

	
    public static Long getLong(Map<String, String> map, String key) {
        if (null == map || null == key || StringUtils.isBlank(map.get(key))) {
            return null;
        }
        return Long.valueOf(map.get(key));
    }

    public static Integer getInt(Map<String, String> map, String key) {
        if (null == map || null == key || StringUtils.isBlank(map.get(key))) {
            return null;
        }
        return Integer.valueOf(map.get(key));
    }

    public static Date getDate(Map<String, String> map, String key, String format) {
    	
        if (null == map || null == key || StringUtils.isBlank(map.get(key))) {
            return null;
        }
        return DateUtil.getDateByStrType(map.get(key), format);
    }

    public static String getString(Map<String, String> map, String key) {
        if (null == map || null == key) {
            return "";
        }
        Object value = map.get(key);
        if (null == value) {
            return "";
        }
        if (value instanceof Timestamp) {
            Timestamp t = (Timestamp) value;
            return t.toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
        if (value instanceof BigDecimal) {
            BigDecimal b = (BigDecimal) value;
            return b.toString();
        }
        return value.toString();
    }

    public static String toJsonString(Map<String, Object> map, boolean null2emptyString) {
        StringBuilder result = new StringBuilder("{");
        if (null != map) {
            StringBuilder lv1 = new StringBuilder();
            Set<String> keys = map.keySet();
            for (String key : keys) {
                if (lv1.length() > 0) {
                    lv1.append(",");
                }

                Object value = map.get(key);
                if (null == value && null2emptyString) {
                    value = "";
                }

                if (value instanceof String) {
                    lv1.append("\"" + key + "\":\"").append((String) value).append("\"");
                } else if (value instanceof Integer) {
                    lv1.append("\"" + key + "\":").append((Integer) value);
                } else if (value instanceof Long) {
                    lv1.append("\"" + key + "\":").append((Long) value);
                } else if (value instanceof Map) {
                    lv1.append("\"" + key + "\":").append(toJsonString((Map) value, null2emptyString));
                }
            }
            result.append(lv1);
        }
        result.append("}");
        return result.toString();
    }
}
